Title: SQL W3S (By-BKK) / 113 Default
Tags: Obsidian_to_Anki

Q: What does the SQL DEFAULT constraint do?
A: It sets a default value for a column when no value is specified

Q: True or False: The DEFAULT value will be added to all new records if no other value is specified.
A: True

Q: How can you set a DEFAULT value for a column when creating a table?
A: By adding DEFAULT keyword and value in the column definition

Q: Show how to set the DEFAULT value for the “City” column to ‘Sandnes’ when creating a table.
A: City varchar(255) DEFAULT 'Sandnes'

Q: What SQL statement adds a DEFAULT value to the “OrderDate” column using a system function during table creation?
A: OrderDate date DEFAULT GETDATE()

Q: How to add a DEFAULT constraint to an existing column in MySQL?
A: ALTER TABLE Persons ALTER City SET DEFAULT 'Sandnes'

Q: How to add a DEFAULT constraint in SQL Server if the table already exists?
A: ALTER TABLE Persons ADD CONSTRAINT df_City DEFAULT 'Sandnes' FOR City

Q: How to add a DEFAULT constraint in MS Access when the table already exists?
A: ALTER TABLE Persons ALTER COLUMN City SET DEFAULT 'Sandnes'

Q: How to add a DEFAULT constraint to an existing column in Oracle?
A: ALTER TABLE Persons MODIFY City DEFAULT 'Sandnes'

Q: How do you drop a DEFAULT constraint in MySQL?
A: ALTER TABLE Persons ALTER City DROP DEFAULT

Q: How do you drop a DEFAULT constraint in SQL Server, Oracle, or MS Access?
A: ALTER TABLE Persons ALTER COLUMN City DROP DEFAULT

Q: The DEFAULT constraint can use system functions for default values.
A: True

Q: Answer first: Sets a default value for a column if no value is provided. What is this called in SQL?
A: DEFAULT constraint

Q: Answer first: ALTER TABLE Persons ALTER City SET DEFAULT 'Sandnes' What does this SQL statement do?
A: It sets the DEFAULT value of the “City” column to ‘Sandnes’ in MySQL

Q: True or False: The DEFAULT constraint prevents NULL values.
A: False

Q: You should use the DEFAULT constraint when you want all new rows to get a specific value unless a different value is given.
A: True

Q: What is the main advantage of using DEFAULT constraints in SQL tables?
A: It ensures consistency by automatically filling columns with default values if none are provided